<?php

include _ITSTOCK_INCLUDE_DIR_.'html_menu.inc.php';

$itstock_page_info['css'] = array_merge($itstock_page_info['css'], array('aside.css', 'float_menu.css'));
$itstock_page_info['js'] = array_merge($itstock_page_info['js'], array('common.js', 'menu.js'));

itstock_html_base_header($itstock_page_info);

itstock_structure($buildings, 'building');
itstock_structure($bldg, 'building');
$menu_title = array(
  'text' => 'All Buildings',
  'url' => 'main.php'
);
$bldg_get = $_GET;
if(isset($bldg_get['idx'])) unset($bldg_get['idx']);
if(isset($bldg_get['search'])) unset($bldg_get['search']);
if(isset($_GET['bldg'])) {
  $buildings['where'][0] = 'id='.itstock_decode($_GET['bldg']);
}
echo '<div class="aside">';
itstock_html_menu_struct($bldg, 'building', 'id', 'bldg', '', $menu_title, $bldg_get);
echo '</div>';

/* Common Structs */
itstock_structure($conditions, 'condition');
itstock_search_all($conditions);
itstock_structure($cats, 'category');
itstock_search_all($cats);
$bldg_filer = isset($_GET['bldg']) && itstock_search_all($buildings);
if($bldg_filer) $building = &$buildings['data'][0];
?><div class="bside">
  <h2>Summary</h2>
  <br><h3>
  <?php
    echo ($bldg_filer?"$building[building]":"All Hardware");
    ?>
  </h3><br>
  <table class="table table-striped">
    <thead><tr>
      <th>Hardware</th>
      <?php
      foreach($conditions['data'] as &$conditon){
        echo '<th>'.$conditon['state'].'</th>';
      }
      ?>
      <th>Total</th>
    </tr></thead>
    <tbody>
    <?php
      itstock_structure($hws, 'hardware');
      unset($hws['order']);
      foreach($cats['data'] as &$cat){
        echo '<tr><td><b>'.$cat['category'].'</b></td>';
        $hws['where'][0] = 'category_id='.$cat['id'];
        foreach($conditions['data'] as &$conditon){
          $hws['where'][1] = 'condition_id='.$conditon['id'];
          if(itstock_search_count($hws)){
            echo '<td>'.$hws['total'][0]['total'].'</td>';
          }
        }
        unset($hws['where'][1]);
        if(itstock_search_count($hws)){
          echo '<td>'.$hws['total'][0]['total'].'</td>';
        }
        echo '</tr>';
      }
    ?>
    </tbody>
    
        <?php
      itstock_structure($hws, 'hardware_by_building');
      $hws['union'][0]['where'][2] = 'building_id='.$building['id'];
      $hws['union'][1]['where'][2] = &$hws['union'][0]['where'][2];
      foreach($cats['data'] as &$cat){
        echo '<tr><td><b>'.$cat['category'].'</b></td>';
        $hws['union'][0]['where'][0] = 'category_id='.$cat['id'];
        $hws['union'][1]['where'][0] = &$hws['union'][0]['where'][0];
        foreach($conditions['data'] as &$conditon){
          $hws['union'][0]['where'][1] = 'condition_id='.$conditon['id'];
          $hws['union'][1]['where'][1] = &$hws['union'][0]['where'][1];
          if(itstock_search_count($hws)){
            echo '<td>'.($hws['total'][0]['total']+$hws['total'][1]['total']).'</td>';
          }
        }
        unset($hws['union'][0]['where'][1], $hws['union'][1]['where'][1]);
        if(itstock_search_count($hws)){
          echo '<td>'.($hws['total'][0]['total']+$hws['total'][1]['total']).'</td>';
        }
        echo '</tr>';
      }
    ?>
    </tbody>
    
    <tfoot><tr>
      <th>Total</th>
      <?php
      foreach($conditions['data'] as &$conditon){
        $hws['where'][0] = 'condition_id='.$conditon['id'];
        if(itstock_search_count($hws)) {
            echo '<th>'.$hws['total'][0]['total'].'</th>';
        }
      }
      unset($hws['where'][0]);
      if(itstock_search_count($hws)){
        echo '<th>'.$hws['total'][0]['total'].'</th>';
      }
      ?>
    </tr></tfoot>
  </table>
  <br>
  <h3>Cubicles</h3>
  <br>
  <table class="table table-striped">
    <thead><tr>
      <th>&nbsp;</th>
      <th>Complete</th>
      <th>Incomplete</th>
      <th>Total</th>
    </tr></thead>
    <tbody>
  <?php
    itstock_search($cases, 
    "SELECT IF( (
        SELECT COUNT( DISTINCT mdl.category_id )
        FROM hardware AS hw
        LEFT JOIN (
          models AS mdl, categories AS cat
        ) ON ( hw.model_id = mdl.id
          AND mdl.category_id = cat.id 
        )
        WHERE hw.cpu_id = cpus.id
          AND cat.required =1
          AND cat.outcase =0 
      ) = (
        SELECT COUNT( * )
        FROM categories
        WHERE outcase =0
          AND required =1
      ) , 1, 0 ) AS completed, COUNT( * ) AS total
      FROM cpus
    GROUP BY completed;"
    );
    itstock_search($locations, 
    "SELECT completed, COUNT( * ) AS total
    FROM (
      SELECT IF( (
        SELECT COUNT( DISTINCT mdl.category_id )
        FROM hardware AS hw
        LEFT JOIN (
          models AS mdl, categories AS cat
        ) ON ( hw.model_id = mdl.id
          AND mdl.category_id = cat.id )
        WHERE hw.location_id = locations.id
          AND cat.required =1
          AND cat.outcase =1
      ) = (
        SELECT COUNT( * )
        FROM categories
        WHERE outcase =1
          AND required =1 )
        AND EXISTS (
          SELECT *
          FROM cpus
          WHERE cpu_location_id = locations.id
      ), 1, 0 ) AS completed
      FROM locations
    ) AS t1
    GROUP BY completed"
    );
    $ca_co = 1;
    $ca_in = 0;
    if(isset($cases['data'][0]['completed']) && $cases['data'][0]['completed'] !== '0') {
      $ca_co = 0;
      $ca_in = 1;
    }
    $lo_co = 1;
    $lo_in = 0;
    if(isset($locations['data'][0]['completed']) && $locations['data'][0]['completed'] !== '0') {
      $lo_co = 0;
      $lo_in = 1;
    }
  ?>
  <tr>
    <th>Cubicles</th>
    <td><?php echo isset($locations['data'][$lo_co]['total'])?$locations['data'][$lo_co]['total']:0;?></td>
    <td><?php echo isset($locations['data'][$lo_in]['total'])?$locations['data'][$lo_in]['total']:0;?></td>
    <td><?php echo (isset($locations['data'][$lo_co]['total'])?$locations['data'][$lo_co]['total']:0) + (isset($locations['data'][$lo_in]['total'])?$locations['data'][$lo_in]['total']:0);?></td>
  </tr>
  <tr>
    <th>Computer Cases</th>
    <td><?php echo isset($cases['data'][$ca_co]['total'])?$cases['data'][$ca_co]['total']:0;?></td>
    <td><?php echo isset($cases['data'][$ca_in]['total'])?$cases['data'][$ca_in]['total']:0;?></td>
    <td><?php echo (isset($cases['data'][$ca_co]['total'])?$cases['data'][$ca_co]['total']:0) + (isset($cases['data'][$ca_in]['total'])?$cases['data'][$ca_in]['total']:0);?></td>
  </tr>
  </tbody>
  <tfoot><tr><th colspan="4"></th></tr></tfoot>
  </table>
  <!-- Buildings -->
    <?php
      itstock_structure($hws, 'hardware_by_building');
      $hws['union'][0]['where'][2] = 'building_id='.$building['id'];
      $hws['union'][1]['where'][2] = &$hws['union'][0]['where'][2];
      foreach($cats['data'] as &$cat){
        echo '<tr><td><b>'.$cat['category'].'</b></td>';
        $hws['union'][0]['where'][0] = 'category_id='.$cat['id'];
        $hws['union'][1]['where'][0] = &$hws['union'][0]['where'][0];
        foreach($conditions['data'] as &$conditon){
          $hws['union'][0]['where'][1] = 'condition_id='.$conditon['id'];
          $hws['union'][1]['where'][1] = &$hws['union'][0]['where'][1];
          if(itstock_search_count($hws)){
            echo '<td>'.($hws['total'][0]['total']+$hws['total'][1]['total']).'</td>';
          }
        }
        unset($hws['union'][0]['where'][1], $hws['union'][1]['where'][1]);
        if(itstock_search_count($hws)){
          echo '<td>'.($hws['total'][0]['total']+$hws['total'][1]['total']).'</td>';
        }
        echo '</tr>';
      }
    ?>
    </tbody>
    <tfoot><tr>
      <th>Total</th>
      <?php
      foreach($conditions['data'] as &$conditon){
        $hws['union'][0]['where'][0] = 'condition_id='.$conditon['id'];
        if(itstock_search_count($hws)) {
            echo '<th>'.($hws['total'][0]['total']+$hws['total'][1]['total']).'</th>';
        }
      }
      unset($hws['union'][0]['where'][0], $hws['union'][1]['where'][0]);
      if(itstock_search_count($hws)){
        echo '<th>'.($hws['total'][0]['total']+$hws['total'][1]['total']).'</th>';
      }
      ?>
    </tr></tfoot>
    <??>
  </table>
   <br>
  <h3>Cubicles</h3>
  <br>
  <table class="table table-striped">
    <thead><tr>
      <th>&nbsp;</th>
      <th>Complete</th>
      <th>Incomplete</th>
      <th>Total</th>
    </tr></thead>
    <tbody>
  <?php
    itstock_search($cases, 
    "SELECT completed, COUNT( * ) AS total
    FROM (
      SELECT IF( (
        SELECT COUNT( DISTINCT mdl.category_id )
        FROM hardware AS hw
        LEFT JOIN (
          models AS mdl, categories AS cat
        ) ON ( hw.model_id = mdl.id
          AND mdl.category_id = cat.id
        )
        WHERE hw.cpu_id = cpus.id
          AND cat.required =1
          AND cat.outcase =0 
      ) = (
        SELECT COUNT( * )
        FROM categories
        WHERE outcase =0
          AND required =1
      ) , 1, 0 ) AS completed
      FROM cpus LEFT JOIN (locations, departments
      ) ON (cpu_location_id=locations.id
        AND department_id=departments.id
      )
      WHERE building_id=$building[id]
    ) AS t1
    GROUP BY completed;"
    );
    itstock_search($locations, 
    "SELECT completed, COUNT( * ) AS total
    FROM (
      SELECT IF( (
        SELECT COUNT( DISTINCT mdl.category_id )
        FROM hardware AS hw
        LEFT JOIN (
          models AS mdl, categories AS cat
        ) ON ( hw.model_id = mdl.id
          AND mdl.category_id = cat.id )
        WHERE hw.location_id = locations.id
          AND cat.required =1
          AND cat.outcase =1
      ) = (
        SELECT COUNT( * )
        FROM categories
        WHERE outcase =1
          AND required =1 )
        AND EXISTS (
          SELECT *
          FROM cpus
          WHERE cpu_location_id = locations.id
      ), 1, 0 ) AS completed
      FROM locations LEFT JOIN (departments
      ) ON ( department_id=departments.id )
      WHERE building_id=$building[id]
    ) AS t1
    GROUP BY completed"
    );
    $ca_co = 1;
    $ca_in = 0;
    if(isset($cases['data'][0]['completed']) && $cases['data'][0]['completed'] !== '0') {
      $ca_co = 0;
      $ca_in = 1;
    }
    $lo_co = 1;
    $lo_in = 0;
    if(isset($locations['data'][0]['completed']) && $locations['data'][0]['completed'] !== '0') {
      $lo_co = 0;
      $lo_in = 1;
    }
  ?>
  <tr>
    <th>Cubicles</th>
    <td><?php echo isset($locations['data'][$lo_co]['total'])?$locations['data'][$lo_co]['total']:0;?></td>
    <td><?php echo isset($locations['data'][$lo_in]['total'])?$locations['data'][$lo_in]['total']:0;?></td>
    <td><?php echo (isset($locations['data'][$lo_co]['total'])?$locations['data'][$lo_co]['total']:0) + (isset($locations['data'][$lo_in]['total'])?$locations['data'][$lo_in]['total']:0);?></td>
  </tr>
  <tr>
    <th>Computer Cases</th>
    <td><?php echo isset($cases['data'][$ca_co]['total'])?$cases['data'][$ca_co]['total']:0;?></td>
    <td><?php echo isset($cases['data'][$ca_in]['total'])?$cases['data'][$ca_in]['total']:0;?></td>
    <td><?php echo (isset($cases['data'][$ca_co]['total'])?$cases['data'][$ca_co]['total']:0) + (isset($cases['data'][$ca_in]['total'])?$cases['data'][$ca_in]['total']:0);?></td>
  </tr>
  </tbody>
  <tfoot><tr><th colspan="4"></th></tr></tfoot>
  </table>
  <?php
  ?>
  </div>
<?php
itstock_html_base_footer();
?>
